﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GameServer.Core.FSM;
using Proto;
using Summer;

namespace GameServer.Model
{
    //在MMO世界进行同步的实体
    public class Entity
    {
        private int id;
        private Vector3Int position;    //位置
        private Vector3Int direction;   //方向
        private int Entityspeed;   //方向
        private long _lastUpdate;//最后一次更新位置的时间戳
        public EntityState State;
        public int entityId { get { return id; } }
        public int EntitySpeed { get { return Entityspeed; }set { Entityspeed = value; } }
        public Vector3Int Position
        {
            get { return position; }
            set { position = value; _lastUpdate = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            }
        }
        public Vector3Int Direction
        {
            get { return direction; }
            set { direction = value; }
        }



        public Entity(int id,Vector3Int position,Vector3Int direction,int speed)
        {
            this.id = id;
            this.position = position;
            this.direction = direction;
            this.EntitySpeed = speed;
        }
        public float PositionTime
        {
            get
            {
                return (DateTimeOffset.Now.ToUnixTimeMilliseconds() - _lastUpdate) * 0.001f;
            }
        }

        public Proto.NEntity GetData()
        {
            var data = new Proto.NEntity();
            data.Id = id;
            data.Position = new Proto.NVector3() { X = Position.x, Y = Position.y, Z = Position.z };
            data.Direction = new Proto.NVector3() { X = direction.x, Y = -direction.y, Z = -direction.z };
            return data;
        }
        public void SetEntityData(Proto.NEntity entity,EntityState state)
        {
            position.x = entity.Position.X;
            position.y = entity.Position.Y;
            position.z = entity.Position.Z;
            direction.x = entity.Direction.X;
            direction.y = entity.Direction.Y;
            direction.z = entity.Direction.Z;
            State = state;
            _lastUpdate = DateTimeOffset.Now.ToUnixTimeMilliseconds();
        }
        public virtual void Update() 
        { 
        }
    }
}
